var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
        isload: 0,
        number:1,
		address: [],
		usescore: 0,
		dkmoney:0,
		totalprice: '0.00',
		couponvisible:false,
		cuxiaovisible:false,
		bid:0,
		nowbid:0,
		needaddress:1,
		linkman:'',
		tel:'',
		pstimeDialogShow:false,
		pstimeIndex:-1,
		jianmoney:0,
		cxid:0,
	},
	_onLoad: function () {
		var that = this;
		//获取产品信息
		$.get('yuxun/buy', { id: that.options.id }, function (res) {
			if (res.status == 0){
				if(res.msg){
					$.alert(res.msg,function(){
						if(res.url){
							$.goto(res.url)
						}else{
							$.goback();
						}
					});
				}else if(res.url){
					$.goto(res.url)
				}else{
					$.alert('您没有权限购买该商品');
				}
				return;
			}
			res.isload = 1
			that.setData(res);
			that.calculatePrice();
			
		});
	},
	//积分抵扣
	scoredk: function (e) {
		var usescore = e.detail.value[0];
		if (!usescore) usescore = 0
		this.setData({ usescore: usescore });
		this.calculatePrice();

    },
    //加
	_gwcplus1: function (e) {
		var number = this.data.number + 1;
		this.setData({
			number: this.data.number + 1
		}),this.calculatePrice();
	},
	//减
	_gwcminus1: function (e) {
		var number = this.data.number - 1;
		if (number <= 0) {
			return;
		}
		this.setData({
			number: this.data.number - 1
        }),this.calculatePrice()
	},
	//输入
	_gwcinput1: function (e) {
		var number = parseInt(e.detail.value);
		if (number < 1) return 1;
		this.setData({
			number: number
        }),this.calculatePrice()
	},
	//计算价格
	calculatePrice: function () {
		var that = this;
		that.setData({
            totalprice:that.data.number * that.data.info.price
        })
	},
	//提交并支付
	topay: function () {
		var that = this;
        $.post('yuxun/createOrder',{number:that.data.number,id:that.data.info.id}, function (res) {
            if (res.status == 0) {
                $.error(res.msg);
                return;
            }
            var opt = res.data;
            if($.platform == 'wx'){
                wx.requestPayment({
                    'timeStamp': opt.timeStamp,
                    'nonceStr': opt.nonceStr,
                    'package': opt.package,
                    'signType': 'MD5',
                    'paySign': opt.paySign,
                    'success': function (res) {
                        $.success('付款完成');
                        that.subscribeMessage(function(){
                            setTimeout(function () {
                                $.goto('/pages/my/usercenter');
                            }, 1000)
                        })
                    },
                    'fail': function (res) { }
                })
            }
        },'提交中')
	},
	showCouponList:function(e){
		this.setData({couponvisible:true,bid:e.currentTarget.dataset.bid})
	},
	handleClickMask:function(){
		this.setData({couponvisible:false,cuxiaovisible:false})
	},
	showCuxiaoList:function(e){
		this.setData({cuxiaovisible:true,bid:e.currentTarget.dataset.bid})
	},
	changecx:function(e){
		var that = this
		var cxid = e.detail.value;
		console.log(cxid)
		that.setData({cxid:cxid});
		if(cxid == 0){
			that.setData({cuxiaoinfo:false});return;
		}
		$.post("shop/getcuxiaoinfo",{id:cxid},function(res){
			that.setData({cuxiaoinfo:res})
		})
	},
	chooseCuxiao:function(){
		var that = this;
		var allbuydata = that.data.allbuydata
		var bid = that.data.bid;
		var cxid = that.data.cxid;
		if(cxid == 0){
			allbuydata[bid].cuxiaoid = '';
			allbuydata[bid].cuxiaomoney = 0;
			allbuydata[bid].cuxiaoname = '不使用促销';
		}else{
			var cxtype = that.data.cuxiaoinfo.info.type;
			if(cxtype==1){ //满减
				allbuydata[bid].cuxiaomoney = that.data.cuxiaoinfo.info['money'] * -1;
			}else if(cxtype==1){ //满赠
				allbuydata[bid].cuxiaomoney = 0
			}else{ //加价换购
				allbuydata[bid].cuxiaomoney = that.data.cuxiaoinfo.info['money']
			}
			allbuydata[bid].cuxiaoid = cxid
			allbuydata[bid].cuxiaotype = cxtype
			allbuydata[bid].cuxiaoname = that.data.cuxiaoinfo.info['name']
		}
		this.setData({allbuydata:allbuydata,cuxiaovisible:false});
		this.calculatePrice();
	}
}));